package com.example.notepad.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.notepad.R;
import com.example.notepad.bean.Notepad;
import com.example.notepad.databinding.ActivityEditBinding;
import com.example.notepad.db.MyDbHelper;
import com.example.notepad.utils.TimeUtil;

import java.io.Serializable;
import java.util.Locale;

public class EditActivity extends AppCompatActivity {

    private ActivityEditBinding binding;

    private MyDbHelper myDbHelper;

    private Notepad notepad;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Serializable serializable = getIntent().getSerializableExtra("notepad");
        if (serializable != null) {
            notepad = (Notepad) serializable;
            binding.contentEv.setText(notepad.getContent());
            binding.titleTv.setText("编辑记录");
        }else{
            binding.titleTv.setText("添加记录");
        }

        //binding.titleTv.setText("添加记录");
        binding.backIv.setOnClickListener(v -> finish());

        myDbHelper = new MyDbHelper(this);

        binding.clearIv.setOnClickListener(v -> binding.contentEv.setText(""));

        binding.saveIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = binding.contentEv.getText().toString().trim();
                if (TextUtils.isEmpty(content)){
                    Toast.makeText(EditActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (notepad != null){
                    notepad.setContent(content);
                    notepad.setTime(TimeUtil.getTime());
                    myDbHelper.update(notepad);
                    Toast.makeText(EditActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                }else{
                    notepad = new Notepad();
                    notepad.setContent(content);
                    notepad.setTime(TimeUtil.getTime());
                    myDbHelper.insert(notepad);
                    Toast.makeText(EditActivity.this, "新增成功", Toast.LENGTH_SHORT).show();
                }
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}